/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.hengyue.common.core.mapper;

import com.hengyue.common.core.entity.BaseEntity;
import com.hengyue.exception.ServiceException;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

public interface BaseMapper<D, E> {

    /**
     * DTO转Entity
     *
     * @param dto /
     * @return /
     */
    E toEntity(D dto);

    /**
     * Entity转DTO
     *
     * @param entity /
     * @return /
     */
    D toDto(E entity);

    /**
     * DTO集合转Entity集合
     *
     * @param targetList /
     * @return /
     */
    List<E> toEntity(List<D> targetList);

    /**
     * Entity集合转DTO集合
     *
     * @param entityList /
     * @return /
     */
    List<D> toDto(List<E> entityList);

   default <T extends BaseEntity> T toEntityById(Long id, Class<T> targetClass) {
    try {
        T instance = targetClass.getDeclaredConstructor().newInstance();
        instance.setId(id);
        return instance;
    } catch (Exception e) {
        throw new ServiceException("id转实体创建实体失败");
    }
}

}
